package com.silences.servlet;

import com.alibaba.fastjson.JSON;
import com.silences.pojo.User;
import com.silences.pojo.User;
import com.silences.service.UserService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Map;


@WebServlet("/user")
public class UserServlet extends HttpServlet {
    //创建对象
    UserService userService = new UserService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        doPost(req,resp);
    }
    @Override
    protected void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        //乱码问题统一解决
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //通过method字段识别方法
        String method =req.getParameter("method");
        if(method.equals("getUsers")){
            getUsers(req,resp); //获取商品裂帛啊
        }else if(method.equals("getUser")){
            getUser(req,resp);//获取单个商品裂帛啊
        }else if(method.equals("addUser")){
            addUser(req,resp);
        }else if(method.equals("updateUser")){
            updateUser(req,resp);
        }else if(method.equals("deleteUser")){
            deleteUser(req,resp);
        }
    }



    private void addUser(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String username =req.getParameter("username");
        String password =req.getParameter("password");
        String avatar= req.getParameter("avatar");
        String createdate= req.getParameter("createdate");

        try {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            User user = new User(0,username,password,avatar,sf.parse(createdate));
            Map<String,Object> map= userService.addUsers(user);
            resp.getWriter().write(JSON.toJSON(map).toString());
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    //请求转发 request。getRequestDispatcher(跳转路径).forwad(req,resp)
    //   1  可以携带参数    2  游览器里面路径没有变化
    // 重定向 response.sendirect(跳转路径）
    // 1   不可携带参数，游览器里面路径会发生改变


    private void getUsers(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String searchname = req.getParameter("searchname");
        String pageIndex= req.getParameter("pageIndex");
        String pageSize = req.getParameter("pageSize");
        try {
            Map<String,Object> map = userService.getUsers(searchname, pageIndex, pageSize);

           /* //修改部分 开始
            req.setAttribute("users",map.get("data"));
            //请求转发
            req.getRequestDispatcher("/user2.jsp").forward(req,resp);
            //修改部分 结束*/

            resp.getWriter().write(JSON.toJSON(map).toString());
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private void getUser(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        try {

            Map<String,Object> map= userService.getUser(Integer.parseInt(id));
            resp.getWriter().write(JSON.toJSON(map).toString());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void updateUser(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String id =req.getParameter("id");
        String username =req.getParameter("username");
        String password =req.getParameter("password");
        String avatar= req.getParameter("avatar");
        String createdate= req.getParameter("createdate");

        try {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            User user = new User(Integer.parseInt(id),username,password,avatar,sf.parse(createdate));
            Map<String,Object> map= userService.updateUsers(user);
            resp.getWriter().write(JSON.toJSON(map).toString());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void deleteUser(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        try {
            Map<String,Object> map= userService.deleteUser(Integer.parseInt(id));
            resp.getWriter().write(JSON.toJSON(map).toString());
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
